package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.UserInfoUtils;
import com.diudiutechnology.www.yadiudiu.util.view.ChangeAddressPopwindow;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MineFragmentModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/24.
 * 现居地在
 */

public class InTheAddressActivity extends Activity implements View.OnClickListener {
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.provincial)//地址
    TextView provincial;

    @Bind(R.id.address)//详细地址
    EditText address;

    @Bind(R.id.button)//提交
            Button button;

    @Bind(R.id.provincial_layout)//选择城市
            RelativeLayout provincial_layout;

    Context context;
     String currentprovince="";//现居住省

    String  currentcity= "";//现居住市

    String  currentdist="";//现居住区

    String  currentsuite= "";//现居住详细地址
    private MyProgressDialog mDialog;
    private  View  viewkre;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        viewkre= View.inflate(this, R.layout.activity_in_the_address, null);
        setContentView(viewkre);
        ButterKnife.bind(this);
        context=this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        button.setOnClickListener(this);
        provincial_layout.setOnClickListener(this);
        MineFragmentModel Model = UserInfoUtils.readUserInfo(this);
        if (!Sundry.getStinges(Model.getCurrentdist())){//现居住区
             currentprovince=Model.getCurrentprovince();//现居住省
              currentcity= Model.getCurrentcity();//现居住市
              currentdist=Model.getCurrentdist();//现居住区
              currentsuite= Model.getCurrentsuite();//现居住详细地址
            provincial.setText(currentprovince+","+currentcity+","+currentdist);
            address.setText(currentsuite);
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.provincial_layout:
                processAddress();
                break;

            case R.id.button:
                if (TextUtils.isEmpty(provincial.getText().toString().trim())){
                    ToastUtils.show(context, getResources().getString(R.string.please_provincial));
                    return;
                }
                if (TextUtils.isEmpty(address.getText().toString().trim())){
                    ToastUtils.show(context, getResources().getString(R.string.please_address));
                    return;
                }
                processadd();
                break;

        }
    }
    private void processAddress(){
        ChangeAddressPopwindow mChangeAddressPopwindow = new ChangeAddressPopwindow(context);
        mChangeAddressPopwindow.setAddress("广东", "深圳", "福田区");
        mChangeAddressPopwindow.showAtLocation(viewkre, Gravity.BOTTOM, 0, 0);
        mChangeAddressPopwindow
                .setAddresskListener(new ChangeAddressPopwindow.OnAddressCListener() {

                    @Override
                    public void onClick(String prov, String ci, String area) {
                        // TODO Auto-generated method stub

                        currentprovince=prov;

                                currentcity=ci;
                                currentdist=area;
                                provincial.setText(prov +","+ ci+","+ area);



                    }
                });
    }
    //添加地址
    private void processadd(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
            HttpClient.addPresentAddress(currentprovince,currentcity,currentdist,address.getText().toString().trim(),new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    if (mDialog!=null){
                        mDialog.dismiss();
                    }
                    System.out.println("stringmd5result"+result);
                    List<RegistrationModel> getnote = JsonTools.getunderlying(result);
                    if (getnote!=null||getnote.size()>0){
                        String status = getnote.get(0).getStatus();
                        String getMsg = getnote.get(0).getMsg();
                        if (status.equals("200")){

                            finish();
                           UserInfoUtils.getInstans().updateCurrentsuite(context,address.getText().toString().trim());
                            ToastUtils.show(context, getMsg);
                        }else if (status.equals("105")){
                            Intent intent = new Intent(context, LoginActivity.class);
                            startActivity(intent);
                            ToastUtils.show(context, getMsg);
                        }else {
                            ToastUtils.show(context, getMsg);
                        }
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    if (mDialog!=null){
                        mDialog.dismiss();
                    }
                    ToastUtils.Toastshow(context, getResources().getString(R.string.server));
                }

                @Override
                public void onCancelled(CancelledException cex) {
                    if (mDialog!=null){
                        mDialog.dismiss();
                    }
                    ToastUtils.Toastshow(context, getResources().getString(R.string.server));
                }

                @Override
                public void onFinished() {

                }
            });

        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
}
